<?php

require_once(dirname(__DIR__) . '/api/DB.php');
require_once(dirname(__DIR__) . '/api/response.php');

try {

    $db = new DB();
    $pdo = $db::$pdo;

    // 获取文章ID
    $pathParams = $_SERVER['PATH_INFO'];
    $id = trim($pathParams, '/');

    // 更新观看人数 view
    $sql = 'update articles set view = view + 1 where id = ?';
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$id]);

    // 查询文章详情
    $sql = 'select * from articles where id = ?';
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$id]);
    $res = $stmt->fetch(PDO::FETCH_ASSOC);
    if (empty($res)) {
        throw new Exception('该文章不存在');
    }

    myResponse('', 1, 200, $res);
} catch (Exception $e) {
    myResponse($e->getMessage(), 0, 200);
}